package com.lcfc.attend.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lcfc.attend.entity.SysOffice;
import com.lcfc.attend.entity.vo.ClockHistogram;
import com.lcfc.attend.entity.vo.ClockSituation;
import com.lcfc.attend.entity.vo.DutyMan;
import com.lcfc.attend.entity.vo.EmpStabilityVo;
import com.lcfc.attend.entity.vo.LineVo;
import com.lcfc.attend.entity.vo.MainChart;
import com.lcfc.attend.entity.vo.PunchReport;
import com.lcfc.attend.mapper.ProjectEmpMapper;
import com.lcfc.attend.mapper.SysOfficeMapper;
import com.mysql.jdbc.StringUtils;

@Service
public class ProjectService {

	@Autowired
	private ProjectEmpMapper projectEmpMapper;
	@Autowired
	private SysOfficeMapper sysOfficeMapper;
	/**
	 * 获取人员详情
	 * @param id
	 * @return
	 */
	public List<PunchReport> queryMainReport(String usercode) {
		List<SysOffice> officeList = projectEmpMapper.queryOffices(usercode);
		List<PunchReport> result = new ArrayList<PunchReport>();
		for (SysOffice office : officeList) {
			PunchReport punch = new PunchReport();
			punch.setName(office.getOfficeName());
			punch.setPass(projectEmpMapper.queryTotalByOffice(office.getOfficeCode(),"1").getTotal());
			punch.setFail(projectEmpMapper.queryTotalByOffice(office.getOfficeCode(),"2").getTotal());
			punch.setNone(projectEmpMapper.queryTotalByOffice(office.getOfficeCode(),"3").getTotal());
			punch.setOther(projectEmpMapper.queryTotalByOffice(office.getOfficeCode(),"").getTotal());
			punch.setTotal(punch.getPass() + punch.getFail() + punch.getNone() + punch.getOther());
			result.add(punch);
		}
		return result;
	}
	/**
	 * 获取首页打卡、在岗人数
	 * @param usercode
	 * @return
	 */
	public DutyMan queryManNum(String usercode) {
		DutyMan duty = projectEmpMapper.queryDuryNum(usercode);
		DutyMan punch = projectEmpMapper.queryPunchNum(usercode);
		duty.setPunchNum(punch.getPunchNum());
		return duty;
	}
	/**
	 * 首页饼图
	 * @param usercode
	 * @return
	 */
	public List<MainChart> queryMainChart(String usercode) {
		List<SysOffice> officeList = projectEmpMapper.queryOffices(usercode);
		List<MainChart> result = new ArrayList<MainChart>();
		for (SysOffice office : officeList) {
			MainChart chart = new MainChart();
			chart.setName(office.getOfficeName());
			chart.setClock(projectEmpMapper.queryClock(office.getOfficeCode(),"未打卡").getClock());
			chart.setNoclock(projectEmpMapper.queryNoClock(office.getOfficeCode(),"未打卡").getNoclock());
			result.add(chart);
		}
		return result;
	}
	/**
	 * 获取线路
	 * @param usercode
	 * @return
	 */
	public List<ClockSituation> queryOffice(String usercode) {
		List<SysOffice> officeList = projectEmpMapper.queryOffices(usercode);
		List<ClockSituation> result = new ArrayList<ClockSituation>();
		for (SysOffice office : officeList) {
			ClockSituation clock = new ClockSituation();
			clock.setName(office.getOfficeName());
			clock.setOfficeCode(office.getOfficeCode());
			List<LineVo> lineList = sysOfficeMapper.queryLineByOffice(office.getOfficeCode());
			Set<String> lines = new HashSet<String>();
			for (LineVo line : lineList) {
				if (!StringUtils.isNullOrEmpty(line.getLine())) {
					lines.add(line.getLine().split("_")[0]);
				}
			}
			clock.setLines(new ArrayList<>(lines));
			result.add(clock);
		}
		return result;
	}
	/**
	 * 获取安检公司打卡情况
	 * @param usercode
	 * @return
	 */
	public List<ClockHistogram> queryClockSituation(String officeCodes, String dateFlag,
			String attenStates) {
		if (!StringUtils.isNullOrEmpty(officeCodes)) {
			String[] codes = officeCodes.split(",");
			String str = "";
			for (String office : codes) {
				str += "'" + office + "'" + ",";
			}
			officeCodes = str.substring(0, str.length() - 1);
		}
		Integer normal = 0;
		Integer late = 0;
		Integer early = 0;
		Integer omission = 0;
		Integer noClock = 0;
		if (attenStates.contains("0")) {
			normal = 1;
		}
		if (attenStates.contains("1")) {
			late = 1;
		}
		if (attenStates.contains("2")) {
			early = 1;
		}
		if (attenStates.contains("3")) {
			omission = 1;
		}
		if (attenStates.contains("4")) {
			noClock = 1;
		}
		List<ClockHistogram> list = sysOfficeMapper.queryClockSituation(officeCodes,Integer.parseInt(dateFlag),attenStates,normal,late,early,omission,noClock);
		return list;
	}
	/**
	 * 人员稳定性情况柱状图
	 * @param officeCodes
	 * @param dateFlag
	 * @param standard
	 * @return
	 */
	public List<EmpStabilityVo> queryEmpStability(String officeCodes, String dateFlag, String standardDay) {
		if (!StringUtils.isNullOrEmpty(officeCodes)) {
			String[] offices = officeCodes.split(",");
			String str = "";
			for (String office : offices) {
				str += "'" + office + "'" + ",";
			}
			officeCodes = str.substring(0, str.length() - 1);
		}
		List<EmpStabilityVo> list = sysOfficeMapper.queryEmpStability(officeCodes,dateFlag,Integer.parseInt(standardDay));
		for (EmpStabilityVo emp : list) {
			EmpStabilityVo allToal = sysOfficeMapper.queryAllByOffice(emp.getOfficeCode());
			float ratio = (float)(emp.getTotal())/(float)(allToal.getTotal()) * 100;
			String num = String.valueOf(ratio).length() > 3 ? String.valueOf(ratio).substring(0, 2) : String.valueOf(ratio);
			emp.setRatio(num + "%");
		}
		return list;
	}
	/**
	 * 获取线路
	 * @param usercode
	 * @return
	 */
	public List<LineVo> queryLines() {
		List<LineVo> list = sysOfficeMapper.queryLines();
		return list;
	}
	/**
	 * 获取线路打卡情况
	 * @param officeCodes
	 * @param dateFlag
	 * @param attenStates
	 * @return
	 */
	public List<ClockHistogram> queryLineSituation(String lines, String dateFlag, String attenStates) {
		if (!StringUtils.isNullOrEmpty(lines)) {
			String[] codes = lines.split(",");
			String str = "";
			for (String line : codes) {
				str += line + "|";
			}
			lines = str.substring(0, str.length() - 1);
		}
		Integer normal = 0;
		Integer late = 0;
		Integer early = 0;
		Integer omission = 0;
		Integer noClock = 0;
		if (attenStates.contains("0")) {
			normal = 1;
		}
		if (attenStates.contains("1")) {
			late = 1;
		}
		if (attenStates.contains("2")) {
			early = 1;
		}
		if (attenStates.contains("3")) {
			omission = 1;
		}
		if (attenStates.contains("4")) {
			noClock = 1;
		}
		List<ClockHistogram> list = sysOfficeMapper.queryLineSituation(lines,Integer.parseInt(dateFlag),attenStates,normal,late,early,omission,noClock);
		Map<String,ClockHistogram> clearMap = new HashMap<String,ClockHistogram>();
		for (ClockHistogram clock : list) {
			clearMap.put(clock.getName(), clock);
		}
		List<ClockHistogram> result = new ArrayList<ClockHistogram>();
		for (String key : clearMap.keySet()) {
			result.add(clearMap.get(key));
		}
		return result;
	}
}
